import * as $utils from "@/utils/index";
import {setLoginInfo,getLoginInfo,} from '@/utils/auth'

const state = {
  courseIdList:getLoginInfo('courseIdList') ? JSON.parse(getLoginInfo('courseIdList')) : [],
  checkList:getLoginInfo('checkList') ? JSON.parse(getLoginInfo('checkList')) : [] ,//选中的课程信息
  allPrice:getLoginInfo('allPrice') || 0,//选中课程的价格
}


const mutations = {
  SET_CHECKLIST: (state, checkList) => {
    state.checkList = checkList
  },
  SET_COURSEIDLIST: (state, courseIdList) => {
    state.courseIdList = courseIdList
  },
  ADD_ALLPRICE: (state, price) => {
    state.allPrice = $utils.operationFn().add(state.allPrice,price)
    setLoginInfo('allPrice',state.allPrice)
  },
  SUBTRACT_ALLPRICE: (state, price) => {
    state.allPrice = $utils.operationFn().subtract(state.allPrice,price)
    setLoginInfo('allPrice',state.allPrice)

  },
  RESET_ALLPRICE: (state, allPrice) => {
    state.allPrice = allPrice;
    setLoginInfo('allPrice',allPrice)
  },
}
const actions = {
  changeCheckList({commit},checkList){
    commit('SET_CHECKLIST', checkList);
    setLoginInfo('checkList',JSON.stringify(checkList))
  },
  changeCourseIdList({commit},courseIdList){
    commit('SET_COURSEIDLIST', courseIdList);
    setLoginInfo('courseIdList',JSON.stringify(courseIdList))
  },
  changeAllPrice({commit},{price,status}){
    if (status == 'add'){
      commit('ADD_ALLPRICE', price)
    } else if (status == 'subtract') {
      commit('SUBTRACT_ALLPRICE', price)
    }else {
      commit('RESET_ALLPRICE', price)
    }
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

